package com.msmall.community.question.preparer;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.ViewPreparer;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.Constants;
import com.msmall.community.common.CascadeCollection;
import com.msmall.community.common.model.Commend;
import com.msmall.community.dpt.model.Segment;
import com.msmall.community.question.model.Question;

public class QuestionHotPreparer  implements ViewPreparer{
	private static final Log log = LogFactory.getLog(QuestionHotPreparer.class);
	
	@SuppressWarnings("unchecked")
	public void execute(TilesRequestContext tilesContext, AttributeContext context) {
		try{
			HttpServletRequest request = (HttpServletRequest) tilesContext.getRequest();
			Map<Long,List<Commend>> commends = (Map<Long,List<Commend>>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_QUESTION_COMMEND_HOT);
			request.setAttribute("questionIndexCommendMap", commends);
			
			Map<Long,List<Question>> questionByClickAmount = (Map<Long,List<Question>>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_QUESTION_HOT);
			request.setAttribute("questionIndexHotMap", questionByClickAmount);
			
			CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
			CascadeCollection<Segment> questionSegments = cs.getAllDatas().get(ConstantDB.SEGMENT_ID_QUESTION_NAV);
			request.setAttribute("questionSegments", questionSegments);
		}catch(Exception e){
			log.debug(e);
		}
	}
}
